package cn.dnui_lf_705.dnui_16110100705.Fragment;


import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import cn.dnui_lf_705.dnui_16110100705.R;


/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment {
    protected int layout_file=0;
    private SharedPreferences sp;
    protected FragmentManager childManager;
    private FragmentTransaction transaction;
    protected Context context;
    private ViewPager vp1;
    protected View view=null;
    abstract void initViews();
    abstract void initEvents();
    abstract void initData();
    void setLayout_file(int layout_file){
        this.layout_file=layout_file;
    }
    int getLayout_file(){
        return this.layout_file;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState){
        view = inflater.inflate(getLayout_file(),container,false);
        initViews();
        initEvents();
        initData();
        return view;
    }
    public BaseFragment() {
        // Required empty public constructor
        context = getActivity();
    }

    public String getUser_id(){
        sp = getActivity().getSharedPreferences("yht",Context.MODE_PRIVATE);
        return sp.getString("user_id","");
    }



    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        childManager = getChildFragmentManager();
    }

    protected void addFragment(Fragment fragment){
            transaction = childManager.beginTransaction();
            transaction.add(R.id.fragment_container,fragment);
            transaction.commit();
    }

    @Override
    public void onAttach(Context context) {

        super.onAttach(context);
        this.context = context;
    }

    protected void replaceFragment(Fragment fragment)
    {
            transaction = childManager.beginTransaction();
            transaction.replace(R.id.fragment_container,fragment);
            transaction.commit();
    }

    protected void removeFragment(Fragment fragment)
    {
            transaction = childManager.beginTransaction();
            transaction.remove(fragment);
            transaction.commit();
    }

    public void showtoast(String s)
    {
        Toast.makeText(getContext(),s,Toast.LENGTH_SHORT).show();////////////
    }
}
